<script setup lang="ts">
import { loginGoGo } from '@/apis/user';
import router from '@/router';
import { useUserStore } from '@/stores/user';
import type { FormDataType } from '@/types/user';
import { getCoUserInfo } from '@/utils/auth';
import { ElMessage, type FormInstance, type FormRules } from 'element-plus';
import { ref } from 'vue';
// 定义数据
const formData = ref<FormDataType>(useUserStore().userInfo || { username: 'demo', password: 'Hmzs%001' })
// 表单验证规则
const formRules = ref<FormRules<FormDataType>>({
  username: [{ required: true,message: '请输入账号',trigger: 'blur' }],
  password: [{ required: true,message: '请输入密码',trigger: 'blur' }]
})
// 提交表单
const formLogin = ref<FormInstance>()
const submitForm = () => {
  // 表单验证
  formLogin.value?.validate(async (valid:boolean) => {
    if (valid) {
      try {
        // 判断是否记住用户名
        if (checkboxUser.value) {
          useUserStore().setUserInfo(formData.value)
        } else {
          useUserStore().removeUserInfo()
          console.log(getCoUserInfo())
        }
        // 发起请求 // 登录
        const res = await loginGoGo(formData.value)
        useUserStore().setToken(res.data.token) // 存储token
        ElMessage({
          message: '登录成功',
          type: 'success'
        })
        const redirect = router.currentRoute.value.query.redirect as string
        // 如果存在 redirect，则重定向到该路径
        if (redirect) {
          router.push(redirect)
        } else {
          // 否则重定向到默认路径（例如首页）
          router.push('/')
        }
      } catch (error) {
        ElMessage({
          message: '登录失败',
          type: 'error'
        })
      }
    } else {
      ElMessage({
        message: '请输入正确的账号和密码',
        type: 'error'
      })
    }
  })
}

const checkboxUser = ref(false)
</script>

<template>
  <div class="login_body">
    <div class="bg" />
    <div class="box">
      <div class="title">智慧园区-登录</div>
      <el-form ref="formLogin" :model="formData" :rules="formRules">
        <el-form-item label="账号" prop="username">
          <el-input v-model="formData.username" placeholder="请输入账号" />
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="formData.password" placeholder="请输入密码" />
        </el-form-item>

        <el-form-item prop="remember">
          <el-checkbox v-model="checkboxUser">记住我</el-checkbox>
        </el-form-item>

        <el-form-item>
          <el-button @click="submitForm()" type="primary" class="login_btn">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login_body {
  display: flex;
}

.bg {
  width: 60vw;
  height: 100vh;
  background: url("@/assets/login-bg.svg") no-repeat;
  background-position: right top;
  background-size: cover;
}

.box {
  margin: 200px 10% 0;
  flex: 1;

  .title {
    padding-bottom: 76px;
    font-size: 26px;
    font-weight: 500;
    color: #1e2023;
  }

  ::v-deep() {
    .ant-form-item {
      display: flex;
      margin-bottom: 62px;
      flex-direction: column;
    }

    .ant-form-item-label label {
      font-size: 16px;
      color: #8b929d;
    }

    .ant-input,
    .ant-input-password {
      border-radius: 8px;
    }
  }
}

.login_btn {
  width: 100%;
}
</style>
